/*
 * @Description: 加载主题样式
 * @Author: seadon
 * @LastEditors: seadon
 * @Date: 2022-03-07 21:03:47
 * @LastEditTime: 2022-03-24 16:03:09
 */
const tenantDetailStr = localStorage.getItem('tenantDetail')
let tenantDetail = tenantDetailStr
	? JSON.parse(localStorage.getItem('tenantDetail'))
	: {}
let theme =
	tenantDetail && tenantDetail.data
		? tenantDetail.data.navigation_bg || 'blue'
		: 'blue'
const mapper = {
	'#306EFF': 'blue',
	'#224585': 'darkblue',
	'#CE3B43': 'gray',
	'#06BE87': 'green',
	'#EF762F': 'orange',
}
let themeVar = theme
if (theme.substr(0, 1) === '#') {
	themeVar = mapper[theme]
}
const link = document.createElement('link')
link.setAttribute('type', 'text/css')
link.setAttribute('rel', 'stylesheet')
link.setAttribute(
	'href',
	`${process.env.VUE_APP_CDN_PATH}/lib/byteluck-theme-antdv3/1.1.0/byteluck.theme.' +
		themeVar +
		'.ant.css?t=<%= new Date().getTime() %>"`
)
document.head.appendChild(link)

const setBodyVisible = function () {
	try {
		document.body.style.visibility = 'visible'
	} catch (e) {
		return void setTimeout(setBodyVisible, 50)
	}
}

link.onload = function () {
	if (document.body) {
		document.body.style.visibility = 'visible'
	} else {
		void setBodyVisible()
	}
}
link.onreadystatechange = function () {
	if ('complete' === link.readyState) {
		document.body.style.visibility = 'visible'
	}
}
